#include<assert.h>

namespace wx
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		iterator begin()
		{
			return _start;
		}

		const_iterator begin() const
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator end() const
		{
			return _finish;
		}

		vector(){}

		vector(int n, const T& x = T())
		{
			reserve(n);
			while (it != _start + n)
			{
				*_finish = x;
				_finish++;
			}
		}

		vector(size_t n, const T& x = T())
		{
			reserve(n);
			for(int i = 0 ;i < n ;i++ )
			{
				push_back(x);
			}
		}

		vector(const vector<T>& v)
		{
			reserve(v.size());
			for (auto& e : v)
			{
				push_back(e);
			}
		}

		template<class inputiterator>
		vector(inputiterator first, inputiterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}

		vector(initializer_list<T> il)
		{
			reserve(il.size());
			for (auto& e : il)
			{
				push_back(e);
			}
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}

		vector<T>& operator=(vector<T> tem)
		{
			swap(tem);
			return *this;
		}

		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finish = _end_of_storage = nullptr;
			}
		}

		int capacity() const
		{
			return _end_of_storage - _start;
		}

		int size() const
		{
			return _finish - _start;
		}

		T& operator[](int x)
		{
			assert(x < size());
			return _start[x];
		}

		void resize(size_t n, const T& x = T())
		{
			if (n > capacity())
			{
				reserve(n);
				while (_finish != _start + n)
				{
					*_finish = x;
					_finish++;
				}
			}
			else 
			{
				_finish = _start + n;
			}
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				int old_size = size();
				int newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				T* tem = new T[n];
				for (int i = 0; i < old_size; i++)
				{
					tem[i] = _start[i];
				}
				delete[] _start;
				
			}
			_start = tem;
			_finish = _start + old_size;
			_end_of_storage = _start + n;
		}

		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				int newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcapacity);
			}

			*_finish = x;
			_finish++;
		}

		void pop_back()
		{
			assert(_finish - _start > 0);
			_finish--;
		}

		iterator insert(iterator pos, const T& x)
		{
			if (_finish == _end_of_storage)
			{
				int len = pos - _start;
				int newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcapacity);
				pos = _start + len;
			}

			iterator it = _finish;
			while (it > pos)
			{
				*it = *(it - 1);
				it--;
			}

			*pos = x;
			++_finish;

			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);

			iterator it = pos + 1;
			while (it < _finish)
			{
				*(it - 1) = *it;
				++it;
			}

			--_finish;

			return pos;
		}
	private:
		T* _start;
		T* _finish;
		T* _end_of_storage;
	};
}